/*
 * Copyright (C) 2020 Agrui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.os;


public class MessageData<T> implements Cloneable, IReleaseable {
    private boolean mUsed = false;
    protected static ObjectPool<MessageData> sPool;
    public static synchronized <T extends MessageData> ObjectPool<T> getObjectPool(Class<T> clazz) {
        if (sPool == null) {
            sPool = new ObjectPool<MessageData>();
        }
        return (ObjectPool<T>)sPool;
    }

    public static synchronized <T extends MessageData> ObjectPool<T> getObjectPool(Class<T> clazz, int size) {
        if (sPool == null) {
            sPool = new ObjectPool<MessageData>(size);
        }
        return (ObjectPool<T>)sPool;
    }

    public void markInUse() {
        mUsed = true;
    }

    public void markUnuse() {
        mUsed = false;
    }

    public boolean isInUse() {
        return mUsed;
    }

    @Override
    public void recycle() {
        markUnuse();
        if (sPool != null) {
            sPool.recycle(this);
        }
    }

    @Override
    public MessageData clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException("The Children of MessageData must implement clone()");
    }


}

